﻿using System.Data.SQLite;
using System.IO;

namespace uTrade.Data.DBManage
{
    internal class SQLiteManage
    {
        private SQLiteConnection m_dbConnection;

        public SQLiteManage()
        {
            CreateDatabase();
            ConnectDatabase();
            InitDataTable();
        }

        //创建一个空的数据库

        private void CreateDatabase()
        {
            if (!File.Exists("./Data/uTradeDB.sqlite"))
            {
                SQLiteConnection.CreateFile("./Data/uTradeDB.sqlite");
            }
        }

        //创建一个连接到指定数据库

        private void ConnectDatabase()
        {
            m_dbConnection = new SQLiteConnection("Data Source=./Data/uTradeDB.sqlite;Version=3;");
            m_dbConnection.Open();
        }

        //在指定数据库中创建一个table
        /// <summary>
        /// 从DBInit读取数据库初始化语句
        /// </summary>
        private void InitDataTable()
        {
            string sql = Properties.Resource.DBInit;
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
        }
    }
}